 $(function(){
	 
})

// 医院级别的选择
$('.form-level  label').on('click', function() {
	$('.form-level  label').siblings("label").removeClass('select');
	$(this).addClass('select');
	$('.form-level input').removeAttr('checked');
	var id = $(this).attr('for');
	$('#' + id).attr('checked', 'checked');
});
//医院类型的选择
$('.form-level-report-type label').on('click', function(){
    $('.form-level-report-type label').siblings("label").removeClass('select');
    $(this).addClass('select');
    $('.form-level-report-type input').removeAttr('checked');
    var id=$(this).attr('for');
    $("#"+ id).attr('checked','checked');
});
 
var iscreate=0;//检测是否生成图片完成

$("#"+tabId+" #generateAnalysisBtn").on("click",function() {
	loadTabParam(tabId);
	var param = drgUI.setAjaxData(tabId) ;
	var hospCode=param.hospCode; //医院院区编码
	var isBlank = false;

	$.each($('[required="required"]'), function(i, n) {
		if ($(n).val() == '') {
			setTimeout(function() {
				$(n).removeClass('ue-shake');
			}, 900);
			$(n).addClass('ue-shake');
			isBlank = true;
		}
	});
	if (isBlank)
		return;
	$("#"+tabId+" #generateAnalysisBtn").attr('disabled', 'disabled').find('i').addClass('fa-spin');
	$('#exportResult .panel-footer1').html('<i class="fa fa-cog fa-spin fa-4x fa-fw margin-bottom text-success" style="margin-left:190px;" ></i>');
	
	//医院基础信息
	var hospBuildYear = $('#'+tabId+' #hospBuildYear').val();
	var hospName=$('#'+tabId+' #hospName').val();
	var hospLevel= $('input[name="hospLevel"]:checked ').val();
	var hospType= $('input[name="hospType"]:checked ').val();
	var hospBedNum=$('#'+tabId+' #hospBedNum').val();
	var hospRealBedNum=$('#'+tabId+' #hospRealBedNum').val();
	var hospCaseNum=$('#'+tabId+' #hospCaseNum').val();
	var hospDocNum=$('#'+tabId+' #hospDocNum').val();
	
	var dateFrom = $('#'+tabId+' #sdate').val();
	var dateQuater = '';
	var depCode="" ;
	var isInmedical= $("#"+tabId+" #isInmedical .radio_bg_check input").val();
	var isComplete= $("#"+tabId+" #isCompleteId .radio_bg_check input").val();
	var isMonth= $("#"+tabId+" #isMonthId .radio_bg_check input").val();
	var dateTo = $('#'+tabId+' #sdate').val();
	// 导出基础数据和表格信息benchmarkYearID
	console_wt('获取分析报告基础数据...');
		$.ajax({type : "post",
				url : contextPath+ "/monthAnalisisReport/findAnalysisReportInfo.dhc",
				data : {
					"dateFrom":dateFrom ,
					"dateTo": dateTo ,
					"dateQuater": dateQuater ,
					"isMonth":isMonth,
					"hospName":hospName,
					"hospCode":hospCode,
					"hospBuildYear":hospBuildYear,
					"hospLevel":hospLevel,
					"hospType":hospType,
					"hospBedNum":hospBedNum,
					"hospRealBedNum":hospRealBedNum,
					"hospCaseNum":hospCaseNum,
					"hospDocNum":hospDocNum,
					"depCode":depCode,
					"isInmedical":isInmedical,
					"isComplete":isComplete
				},
				success : function(result) {
					if (result.data.errorMessageData == undefined) {
							var data = result.data;
							$("#t2").show();
							createPicture(data);
							var timer=setInterval(function(){
							    if (iscreate=='1') {
							     clearInterval(timer);
								$.ajax({   
									type : "post",
									url : contextPath+ "/monthAnalisisReport/createAnalysisReport.dhc",
									data : {"isInmedical":isInmedical },
									success : function(result) {
										if(result.success){
											var patch=result.data.patch;
											var hospname=result.data.hospname;
											var reportDate=result.data.reportDate;
											var depName=result.data.depName;
											if(depName != ""){
												depName =depName+'科室';
											}										
											var exportBtnHtml = '<div class="btn-group" role="group" style="margin-left: 150px;">';
											if(hospname.indexOf("医院")>0){
												hospname=hospname.substring(0,hospname.length-2);
											}
											exportBtnHtml += '<a type="button" download="'+ hospname+ '医院'+depName+reportDate+'住院病例DRG分析报告.docx" class="btn btn-default btn-lg" href="'+ patch+ '"><i class=" icon-save"></i>&nbsp;下载分析报告</a>';														
											exportBtnHtml += '</div>';
											$("#"+tabId+" #generateAnalysisBtn").removeAttr('disabled','disabled').find('i').removeClass('fa-spin');
											$('#exportResult .panel-footer1').html(exportBtnHtml);
											drgUI.success('分析报告生成成功！');
										}else{
											var exportBtnHtml =result.errorMessage;
											$("#"+tabId+" #generateAnalysisBtn").removeAttr('disabled','disabled').find('i').removeClass('fa-spin');
											$('#exportResult .panel-footer1').html(exportBtnHtml);	
											drgUI.error(result.errorMessage);
										}
									}
								});
							} 
							   else {
								$("#"+tabId+" #generateAnalysisBtn").removeAttr('disabled', 'disabled').find('i').removeClass('fa-spin');
								$('#exportResult .panel-footer1').html('<p class="text-danger">'+ data.errorMessage+ '</p>');
							}
							},1000)
					} else {
						if (result.data.errorMessageData != undefined) {
							var exportBtnHtml = '<div class="btn-group" role="group">';
							exportBtnHtml += '<span style="color:red;font-size:10px;">'+ result.data.errorMessageData+ '</span>';
							exportBtnHtml += '</div>';
							$("#"+tabId+" #generateAnalysisBtn").removeAttr('disabled', 'disabled').find('i').removeClass('fa-spin');
							$('#exportResult .panel-footer1').html(exportBtnHtml);
						} else {
							var exportBtnHtml = '<div class="btn-group" role="group">';
							exportBtnHtml += '<span style="color:red;font-size:10px;">后台数据报错:'+result.data.errorMsg+'！</span>';
							exportBtnHtml += '</div>';
							$("#"+tabId+" #generateAnalysisBtn").removeAttr('disabled', 'disabled').find('i').removeClass('fa-spin');
							$('#exportResult .panel-footer1').html(exportBtnHtml);
							drgUI.error(result.data.errorMsg);
						}
					}
				}
			});
});

// 生成分析报告所需图表
function createPicture(data) {
	 var depCode=""; //$('#'+tabId+' #depCodeID').val();
     iscreate=0;
	 console_wt('开始生成图表...');
	 console_wt('构建入组率变化趋势图...');	
	 var xData = [];
	 var yData = [];
	 var inCaseRatioList=data.diagram1;
	 if(inCaseRatioList && inCaseRatioList.length>0){
		 for (var i = 0; i < inCaseRatioList.length; i++) {	
			 xData[i]=inCaseRatioList[i].TIMEDIMENTION;
			 yData[i]=inCaseRatioList[i].INCASERATIO;	
		 }
	 }
	 analysisReportLineCharts1('monthHospPicone',$('#monthHospImageOne'), xData,yData,'入组率(%)',0);
	 
	console_wt('构建病例未入组原因分布图...');
	chartData = [];
	for (var i = 0; i < data.diagram2.length; i++) {
		chartData.push({
			name: data.diagram2[i].DECC,
			value: data.diagram2[i].CNT
		});
	}
    analysisReportPieCharts(true,50,'monthHospPictwo',$('#monthHospImageTwo'), chartData, "", "");
    
	console_wt('构建全院病例难度分布图...');
	chartData = [];
	var RWList=data.diagram3
	var yData = [RWList.thisOne.INTERVAL1,RWList.thisOne.INTERVAL2,RWList.thisOne.INTERVAL3,RWList.thisOne.INTERVAL4,RWList.thisOne.INTERVAL5,RWList.thisOne.INTERVAL6];
	var yDataY = [RWList.lastOne.INTERVAL1,RWList.lastOne.INTERVAL2,RWList.lastOne.INTERVAL3,RWList.lastOne.INTERVAL4,RWList.lastOne.INTERVAL5,RWList.lastOne.INTERVAL6];
	var xData = ['0~0.5','0.5~0.8','0.8~1.2','1.2~2','2~5','大于5'];
	var dataName = ['本期','同期'];
	analysisReportAcreageCharts_new('monthHospPicthree',$('#monthHospImageThree'),yData,yDataY,'',dataName,xData,'难度区间','病例数占比(%)','本期','同期','',0);
    
    if(depCode==""){
    	console_wt('构建MDC专业病组覆盖率分布图...');
    	var xData = [];
    	var yData = [];
    	var islegend = ['理论DRGs组数','本期值','同期值','病组覆盖率'];
    	for (var i = 0; i < data.diagram4.length; i++) {
    		xData[i]=data.diagram4[i].code;
    		yData[i]=data.diagram4[i].DrgsCoverageRation;
    	}
    	analysisReportLineCharts('monthHospPicfour',$('#monthHospImageFour'), xData,yData,'覆盖率(%)',90);

    	console_wt('构建MDC专业时间消耗指数和费用消耗指数象限分布图（同比正向变好）...');
        var scatterChartData = [];
    	var xAxis={"desc":"时间消耗指数"};
    	var yAxis={"desc":"费用消耗指数"};
    	var minFeeEff=99999;
    	var maxFeeEff=-99999;
    	var minTimeEff=99999;
    	var maxTimeEff=-99999;
    	for (var i = 0; i < data.diagram8.length; i++) {
    		//求时间消耗指数最小值
    		if(minTimeEff>data.diagram8[i].TimeEffcc){
    			minTimeEff=data.diagram8[i].TimeEffcc;
    		}
    		if(minTimeEff>data.diagram8[i].TimeEff){
    			minTimeEff=data.diagram8[i].TimeEff;
    		}
    		//求时间消耗指数最大值
    		if(maxTimeEff<data.diagram8[i].TimeEffcc){
    			maxTimeEff=data.diagram8[i].TimeEffcc;
    		}
    		if(maxTimeEff<data.diagram8[i].TimeEff){
    			maxTimeEff=data.diagram8[i].TimeEff;
    		}
    		//求费用消耗指数最小值
    		if(minFeeEff>data.diagram8[i].FeeEffcc){
    			minFeeEff=data.diagram8[i].FeeEffcc;
    		}
    		if(minFeeEff>data.diagram8[i].FeeEff){
    			minFeeEff=data.diagram8[i].FeeEff;
    		}
    		//求费用消耗指数最大值
    		if(maxFeeEff<data.diagram8[i].FeeEffcc){
    			maxFeeEff=data.diagram8[i].FeeEffcc;
    		}
    		if(maxFeeEff<data.diagram8[i].FeeEff){
    			maxFeeEff=data.diagram8[i].FeeEff;
    		}
    		scatterChartData.push({"coords":[[data.diagram8[i].TimeEffcc,data.diagram8[i].FeeEffcc],[data.diagram8[i].TimeEff, data.diagram8[i].FeeEff]], value:data.diagram8[i].ccode});
    	}
    	xAxis.xmin=minTimeEff;
    	xAxis.xmax=maxTimeEff;
    	yAxis.ymin=minFeeEff;
    	yAxis.ymax=maxFeeEff;
        linesChartBasic($('#monthHospImageEight'),scatterChartData,xAxis,yAxis,'','','', {"showLabel":true},'monthHospPiceight');
        
        console_wt('构建MDC专业时间消耗指数和费用消耗指数象限分布图（同比负向变差）...');
        var scatterChartData = [];
    	var xAxis={"desc":"时间消耗指数"};
    	var yAxis={"desc":"费用消耗指数"};
    	var minFeeEff=99999;
    	var maxFeeEff=-99999;
    	var minTimeEff=99999;
    	var maxTimeEff=-99999;
    	for (var i = 0; i < data.diagram9.length; i++) {
    		//求时间消耗指数最小值
    		if(minTimeEff>data.diagram9[i].TimeEffcc){
    			minTimeEff=data.diagram9[i].TimeEffcc;
    		}
    		if(minTimeEff>data.diagram9[i].TimeEff){
    			minTimeEff=data.diagram9[i].TimeEff;
    		}
    		//求时间消耗指数最大值
    		if(maxTimeEff<data.diagram9[i].TimeEffcc){
    			maxTimeEff=data.diagram9[i].TimeEffcc;
    		}
    		if(maxTimeEff<data.diagram9[i].TimeEff){
    			maxTimeEff=data.diagram9[i].TimeEff;
    		}
    		//求费用消耗指数最小值
    		if(minFeeEff>data.diagram9[i].FeeEffcc){
    			minFeeEff=data.diagram9[i].FeeEffcc;
    		}
    		if(minFeeEff>data.diagram9[i].FeeEff){
    			minFeeEff=data.diagram9[i].FeeEff;
    		}
    		//求费用消耗指数最大值
    		if(maxFeeEff<data.diagram9[i].FeeEffcc){
    			maxFeeEff=data.diagram9[i].FeeEffcc;
    		}
    		if(maxFeeEff<data.diagram9[i].FeeEff){
    			maxFeeEff=data.diagram9[i].FeeEff;
    		}
    		scatterChartData.push({"coords":[[data.diagram9[i].TimeEffcc,data.diagram9[i].FeeEffcc],[data.diagram9[i].TimeEff, data.diagram9[i].FeeEff]], value:data.diagram9[i].ccode});
    	}
    	xAxis.xmin=minTimeEff;
    	xAxis.xmax=maxTimeEff;
    	yAxis.ymin=minFeeEff;
    	yAxis.ymax=maxFeeEff;
        linesChartBasic($('#monthHospImageNine'),scatterChartData,xAxis,yAxis,'','','', {"showLabel":true},'monthHospPicnine');
        
    	console_wt('构建科室总权重及其出院病例数全院占比分布图（总权重前10）...');
    	var xData = [];
    	var yData1 = [];
    	var yData4 = [];
    	var islegend = ['总权重','出院病例数全院占比(%)'];
    	for (var i = 0; i < data.diagram10.length; i++) {
    		xData[i]={value:data.diagram10[i].desc,textStyle: {fontSize: 18}};
    		yData1[i]=data.diagram10[i].TotalWeight.replace(/,/g,'');
    		yData4[i]=data.diagram10[i].caseRatio;
    	}
        analysisReportBarLineCharts('monthHospPicten',$('#monthHospImageTen'),xData,yData1,yData4,'总权重','出院病例数全院占比(%)                                ','总权重','出院病例数全院占比(%)',islegend);
        
        console_wt('构建科室时间消耗指数和费用消耗指数象限分布图（同比正向变好）...');
        var scatterChartData = [];
    	var xAxis={"desc":"时间消耗指数","fontSize":"{fontSize: 18}"};
    	var yAxis={"desc":"费用消耗指数"};
    	var minFeeEff=99999;
    	var maxFeeEff=-99999;
    	var minTimeEff=99999;
    	var maxTimeEff=-99999;
    	for (var i = 0; i < data.diagram11.length; i++) {
    		//求时间消耗指数最小值
    		if(minTimeEff>data.diagram11[i].TimeEffcc){
    			minTimeEff=data.diagram11[i].TimeEffcc;
    		}
    		if(minTimeEff>data.diagram11[i].TimeEff){
    			minTimeEff=data.diagram11[i].TimeEff;
    		}
    		//求时间消耗指数最大值
    		if(maxTimeEff<data.diagram11[i].TimeEffcc){
    			maxTimeEff=data.diagram11[i].TimeEffcc;
    		}
    		if(maxTimeEff<data.diagram11[i].TimeEff){
    			maxTimeEff=data.diagram11[i].TimeEff;
    		}
    		//求费用消耗指数最小值
    		if(minFeeEff>data.diagram11[i].FeeEffcc){
    			minFeeEff=data.diagram11[i].FeeEffcc;
    		}
    		if(minFeeEff>data.diagram11[i].FeeEff){
    			minFeeEff=data.diagram11[i].FeeEff;
    		}
    		//求费用消耗指数最大值
    		if(maxFeeEff<data.diagram11[i].FeeEffcc){
    			maxFeeEff=data.diagram11[i].FeeEffcc;
    		}
    		if(maxFeeEff<data.diagram11[i].FeeEff){
    			maxFeeEff=data.diagram11[i].FeeEff;
    		}
    		scatterChartData.push({"coords":[[data.diagram11[i].TimeEffcc,data.diagram11[i].FeeEffcc],[data.diagram11[i].TimeEff, data.diagram11[i].FeeEff]], value:data.diagram11[i].ddesc});
    	}
    	xAxis.xmin=minTimeEff;
    	xAxis.xmax=maxTimeEff;
    	yAxis.ymin=minFeeEff;
    	yAxis.ymax=maxFeeEff;
        linesChartBasic($('#monthHospImageEleven'),scatterChartData,xAxis,yAxis,'','','', {"showLabel":true},'monthHospPiceleven');
        
        console_wt('构建科室时间消耗指数和费用消耗指数象限分布图（同比负向变差）...');
        var scatterChartData = [];
    	var xAxis={"desc":"时间消耗指数"};
    	var yAxis={"desc":"费用消耗指数"};
    	var minFeeEff=99999;
    	var maxFeeEff=-99999;
    	var minTimeEff=99999;
    	var maxTimeEff=-99999;
    	for (var i = 0; i < data.diagram12.length; i++) {
    		//求时间消耗指数最小值
    		if(minTimeEff>data.diagram12[i].TimeEffcc){
    			minTimeEff=data.diagram12[i].TimeEffcc;
    		}
    		if(minTimeEff>data.diagram12[i].TimeEff){
    			minTimeEff=data.diagram12[i].TimeEff;
    		}
    		//求时间消耗指数最大值
    		if(maxTimeEff<data.diagram12[i].TimeEffcc){
    			maxTimeEff=data.diagram12[i].TimeEffcc;
    		}
    		if(maxTimeEff<data.diagram12[i].TimeEff){
    			maxTimeEff=data.diagram12[i].TimeEff;
    		}
    		//求费用消耗指数最小值
    		if(minFeeEff>data.diagram12[i].FeeEffcc){
    			minFeeEff=data.diagram12[i].FeeEffcc;
    		}
    		if(minFeeEff>data.diagram12[i].FeeEff){
    			minFeeEff=data.diagram12[i].FeeEff;
    		}
    		//求费用消耗指数最大值
    		if(maxFeeEff<data.diagram12[i].FeeEffcc){
    			maxFeeEff=data.diagram12[i].FeeEffcc;
    		}
    		if(maxFeeEff<data.diagram12[i].FeeEff){
    			maxFeeEff=data.diagram12[i].FeeEff;
    		}
    		scatterChartData.push({"coords":[[data.diagram12[i].TimeEffcc,data.diagram12[i].FeeEffcc],[data.diagram12[i].TimeEff, data.diagram12[i].FeeEff]], value:data.diagram12[i].ddesc});
    	}
    	xAxis.xmin=minTimeEff;
    	xAxis.xmax=maxTimeEff;
    	yAxis.ymin=minFeeEff;
    	yAxis.ymax=maxFeeEff;
        linesChartBasic($('#monthHospImageTwelve'),scatterChartData,xAxis,yAxis,'','','', {"showLabel":true},'monthHospPictwelve');
        
    	console_wt('构建手术与非手术占比图...');
    	chartData = [];
   	   	 if(data.diagram1new.opecaseCntR!=0){
 	 	    chartData.push({name : '手术',value : data.diagram1new.opecaseCntR})
 	 	   }
   	   	 if(data.diagram1new.noOpecaseCntR!=0){
  	 	    chartData.push({name : '非手术',value : data.diagram1new.noOpecaseCntR})
  	 	   }
        analysisReportPieCharts(true,50,'monthHospPiconenew',$('#monthHospImageOneNew'), chartData, "", "");
        
    	console_wt('构建各级手术分布占比图...');
    	chartData = [];
    	for (var i = 0; i < data.diagram2new.length; i++) {
    		chartData.push({
    			name: data.diagram2new[i].desc,
    			value: data.diagram2new[i].value
    		});
    	}  	
	 	analysisReportPieCharts(true,50,'monthHospPictwonew',$('#monthHospImageTwoNew'), chartData, "", "");
    
    
    }else{
   	 console_wt('构建科室DRG病组有效占比变化趋势图...');	
	 var xData = [];
	 var yData = [];
	 
	 for (var i = 0; i < data.diagram14.length; i++) {	
		 xData[i]=data.diagram14[i].TIMETYPE;
		 yData[i]=data.diagram14[i].DRGCNTINTHEORYRATIO;	
	 }
	 analysisReportLineCharts('monthHospPicfourteen',$('#monthHospImageFourteen'), xData,yData,'DRG病组有效占比(%)');
    }

	iscreate=1;
	console_wt('图表构建完毕！');
	return true;
}
function console_wt(s) {
	$('#exportResult .panel-footer1').append('<p style="text-align:center;">' + s + '</p>');
}